{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<font color=\"#a9a56c\" size=2> **@Author: Arif Kasim Rozani - (Team Operation Badar)** </font>"
      ],
      "metadata": {
        "id": "38_QpUXJOAII"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# **The Date & Time**\n",
        "----\n",
        "\n",
        "## **What are Tick Intervals**\n",
        "\n",
        "Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970.(epoch=a particular period of time in history or a person's life.).\n"
      ],
      "metadata": {
        "id": "hThYABBt6eH3"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **What is Epoch?**\n",
        "\n",
        "*Sounds: ee·pok (British), eh·puhk (American)*\n",
        "\n",
        "<br>\n",
        "\n",
        "In Python, the `epoch` is a reference `point in time` from which time is measured. It's the moment when time `\"begins\"` for a particular system or standard.\n",
        "\n",
        "Think of it like the zero point on a number line.\n",
        "\n",
        "The most commonly used epoch in Python (and many other systems) is:\n",
        "\n",
        "**`January 1, 1970, 00:00:00 (UTC)`**\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "## **Why is there an epoch?**\n",
        "\n",
        "\n",
        "- **Consistent Timekeeping**: Using a fixed point in the past allows computers and systems to calculate and compare time in a standardized way, regardless of when the system was built or where it's located.\n",
        "\n",
        "- **Simplified Calculations**: It's easier to perform calculations with a single number representing the seconds (or milliseconds, etc.) that have passed since a fixed point, rather than dealing with complex date and time formats.\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "## **How is the epoch used in Python?**\n",
        "\n",
        "Python's time module provides functions to work with time based on the epoch:\n",
        "\n",
        "- **`time.time()`**: Returns the current time as a floating-point number of seconds since the epoch.\n",
        "- **`time.gmtime(seconds)`**: Converts seconds since the epoch to a time tuple in Coordinated Universal Time (UTC).\n",
        "- **`time.localtime(seconds)`**: Converts seconds since the epoch to a time tuple in local time."
      ],
      "metadata": {
        "id": "JGpqqFJWDFMD"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wPeSN5HU6cV6",
        "outputId": "8cfd2327-31a4-4fd3-ef50-63fb4b43e865"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Number of ticks since 12:00am, January 1, 1970: 1737690101.884116\n"
          ]
        }
      ],
      "source": [
        "import time # This is required to include time module.\n",
        "ticks = time.time()\n",
        "print(\"Number of ticks since 12:00am, January 1, 1970:\", ticks)"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Getting the Current Time**\n",
        "----\n",
        "To translate a time instant from seconds since the epoch floating-point value into a time-tuple, pass the floating-point value to a function (e.g., localtime) that returns a time-tuple with all valid nine items."
      ],
      "metadata": {
        "id": "faUJMAtz8bp9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import time\n",
        "localtime = time.localtime(time.time())\n",
        "print (\"Local current time :\", localtime)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cKIJBhj08d0t",
        "outputId": "58f017d1-0444-462c-e762-54a24fd78698"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Local current time : time.struct_time(tm_year=2025, tm_mon=1, tm_mday=24, tm_hour=3, tm_min=41, tm_sec=48, tm_wday=4, tm_yday=24, tm_isdst=0)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Getting the Formatted Time**\n",
        "----\n",
        "You can format any time as per your requirement, but a simple method to get time in a readable format is asctime() −"
      ],
      "metadata": {
        "id": "22rZI4Zr8s_r"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import time\n",
        "localtime = time.asctime( time.localtime(time.time()) )\n",
        "print (\"Local current time :\", localtime)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "e5cjFBXE8x3L",
        "outputId": "efe244fe-fbb6-458d-ef6f-5a0ce96bf31a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Local current time : Thu Jan 23 20:55:57 2025\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# **The Calendar**\n",
        "----"
      ],
      "metadata": {
        "id": "mafkub5a8-Sj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Getting the Calendar for a Month**\n",
        "----\n",
        "The calendar module gives a wide range of methods to play with yearly and monthly calendars. Here, we print a calendar for a given month (Jan 2025)."
      ],
      "metadata": {
        "id": "WjPF9aw99IDL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import calendar\n",
        "cal = calendar.month(2025, 1)\n",
        "print (\"Here is the calendar:\")\n",
        "print (cal)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "E4MDSYME9KCz",
        "outputId": "0ede5640-9d35-42b8-b45e-f7d0ad762231"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Here is the calendar:\n",
            "    January 2025\n",
            "Mo Tu We Th Fr Sa Su\n",
            "       1  2  3  4  5\n",
            " 6  7  8  9 10 11 12\n",
            "13 14 15 16 17 18 19\n",
            "20 21 22 23 24 25 26\n",
            "27 28 29 30 31\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **The Date Time**\n",
        "----"
      ],
      "metadata": {
        "id": "6PmO6wpN9kH6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from datetime import date\n",
        "\n",
        "date1 = date(2023, 4, 19)\n",
        "print(\"Date:\", date1)\n",
        "date2 = date(2023, 4, 30)\n",
        "print(\"Date2:\", date2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MhexnRFS9i6K",
        "outputId": "b9c32266-543a-41e4-d04d-75fd5174efaf"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Date: 2023-04-19\n",
            "Date2: 2023-04-30\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import datetime\n",
        "\n",
        "x = datetime.datetime.now() #The date contains year, month, day, hour, minute, second, and microsecond.\n",
        "print(x)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "arUQEXFE9627",
        "outputId": "e47d3f17-481e-4049-b7af-515f8acc0f47"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2025-01-23 21:01:49.119953\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **The strftime() Method**\n",
        "----\n",
        "The datetime object has a method for formatting date objects into readable strings.\n",
        "\n",
        "The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:"
      ],
      "metadata": {
        "id": "XCBwRYjg-efa"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import datetime\n",
        "\n",
        "x = datetime.datetime(2025, 1, 1)\n",
        "\n",
        "print(x.strftime(\"%f\")) #Display Microsecond 000000-999999\n",
        "print(x.strftime(\"%A\")) #Display the name of the Day\n",
        "print(x.strftime(\"%Y\")) #Display the Year\n",
        "print(x.strftime(\"%B\")) #Display the name of the month\n",
        "\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZfskIuHH-kYp",
        "outputId": "55f108c6-793c-4032-e0ec-4546cf79a1db"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "000000\n",
            "Wednesday\n",
            "2025\n",
            "January\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "A reference of all the legal format codes:\n",
        "[w3school](https://www.w3schools.com/python/python_datetime.asp)"
      ],
      "metadata": {
        "id": "ytq-DmWC-v06"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# **Python Math Module**\n",
        "----\n",
        "The math module is a built-in module in Python that is used for performing mathematical operations. This module provides various built-in methods for performing different mathematical tasks.\n",
        "\n",
        "Let see the different functions in action"
      ],
      "metadata": {
        "id": "Ssrx4LceWsP4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "\n",
        "# abs() returns the absolute value of a number\n",
        "# In Python, abs() is a built-in function, which means it's a global function\n",
        "# available in the standard library without needing to import any specific modules.\n",
        "# You can use it directly in your code.\n",
        "print(\"abs(-5) = \", abs(-5))  # outputs: 5\n",
        "\n",
        "# pow() raises a number to a power\n",
        "print(\"pow(2, 3) = \", pow(2, 3))  # outputs: 8\n",
        "\n",
        "# round() rounds a number to a specified number of decimal places\n",
        "print(\"round(3.14159, 2) = \", round(3.14159, 2))  # outputs: 3.14\n",
        "\n",
        "# max() returns the largest of a set of numbers\n",
        "print(\"max(1, 2, 3, 4, 5) = \", max(1, 2, 3, 4, 5))  # outputs: 5\n",
        "\n",
        "# min() returns the smallest of a set of numbers\n",
        "print(\"min(1, 2, 3, 4, 5) = \", min(1, 2, 3, 4, 5))  # outputs: 1\n",
        "\n",
        "# math.sin() returns the sine of an angle in radians\n",
        "print(\"math.sin(math.pi/2) = \", math.sin(math.pi/2))  # outputs: 1.0\n",
        "\n",
        "# math.cos() returns the cosine of an angle in radians\n",
        "print(\"math.cos(0) = \", math.cos(0))  # outputs: 1.0\n",
        "\n",
        "# math.tan() returns the tangent of an angle in radians\n",
        "print(\"math.tan(math.pi/4) = \", math.tan(math.pi/4))  # outputs: 1.0\n",
        "\n",
        "# math.sqrt() returns the square root of a number\n",
        "print(\"math.sqrt(9) = \", math.sqrt(9))  # outputs: 3.0\n",
        "\n",
        "# math.factorial() returns the factorial of a number\n",
        "print(\"math.factorial(5) = \", math.factorial(5))  # outputs: 120\n",
        "\n",
        "# math.log() returns the natural logarithm of a number\n",
        "print(\"math.log(10) = \", math.log(10))  # outputs: 2.302585092994046\n",
        "\n",
        "# math.log10() returns the base-10 logarithm of a number\n",
        "print(\"math.log10(100) = \", math.log10(100))  # outputs: 2.0\n",
        "\n",
        "# math.exp() returns the value of e raised to a power\n",
        "print(\"math.exp(2) = \", math.exp(2))  # outputs: 7.38905609893065\n",
        "\n",
        "# math.ceil() returns the smallest integer greater than or equal to a number\n",
        "print(\"math.ceil(4.7) = \", math.ceil(4.7))  # outputs: 5\n",
        "\n",
        "# math.floor() returns the largest integer less than or equal to a number\n",
        "print(\"math.floor(4.7) = \", math.floor(4.7))  # outputs: 4\n",
        "\n",
        "# math.pi is a constant representing the ratio of a circle's circumference to its diameter\n",
        "print(\"math.pi = \", math.pi)  # outputs: 3.14159265359\n",
        "\n",
        "# math.e is a constant representing the base of the natural logarithm\n",
        "print(\"math.e = \", math.e)  # outputs: 2.718281828459045\n",
        "\n",
        "# math.tau is a constant representing the ratio of a circle's circumference to its radius\n",
        "print(\"math.tau = \", math.tau)  # outputs: 6.283185307179586\n",
        "\n",
        "# math.inf is a constant representing infinity\n",
        "print(\"math.inf = \", math.inf)  # outputs: inf\n",
        "\n",
        "# math.nan is a constant representing \"not a number\"\n",
        "print(\"math.nan = \", math.nan)  # outputs: nan"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lQcq0UARYamQ",
        "outputId": "24b373da-3b03-4fad-fdb3-4a0c00761c3e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "abs(-5) =  5\n",
            "pow(2, 3) =  8\n",
            "round(3.14159, 2) =  3.14\n",
            "max(1, 2, 3, 4, 5) =  5\n",
            "min(1, 2, 3, 4, 5) =  1\n",
            "math.sin(math.pi/2) =  1.0\n",
            "math.cos(0) =  1.0\n",
            "math.tan(math.pi/4) =  0.9999999999999999\n",
            "math.sqrt(9) =  3.0\n",
            "math.factorial(5) =  120\n",
            "math.log(10) =  2.302585092994046\n",
            "math.log10(100) =  2.0\n",
            "math.exp(2) =  7.38905609893065\n",
            "math.ceil(4.7) =  5\n",
            "math.floor(4.7) =  4\n",
            "math.pi =  3.141592653589793\n",
            "math.e =  2.718281828459045\n",
            "math.tau =  6.283185307179586\n",
            "math.inf =  inf\n",
            "math.nan =  nan\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **NaN**\n",
        "\n",
        "In Python, `NaN` stands for `\"Not a Number\"`. It's a special floating-point value that represents an undefined or unrepresentable numerical result. It's a way for Python to indicate that a calculation or operation couldn't produce a valid numerical outcome.\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "qWGYgLF-GkJG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Uncomment to see error\n",
        "\n",
        "# result = 0 / 0  # Division by zero\n",
        "# print(result)      # Output: nan\n",
        "\n",
        "# result = math.sqrt(-1)  # Square root of a negative number\n",
        "# print(result)      # Output: nan\n",
        "\n",
        "result = float('nan')  # Explicitly creating NaN\n",
        "print(result)      # Output: nan"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ufV5_T0IGkdt",
        "outputId": "07d3a588-9e41-4cf6-c5ed-360061acdffd"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "nan\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Working with NaN:**\n",
        "\n",
        "\n",
        "Checking for NaN: You can use the math.isnan() function to check if a value is NaN"
      ],
      "metadata": {
        "id": "xl20YAguHeBu"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "\n",
        "x = float('nan')\n",
        "if math.isnan(x):\n",
        "  print(\"x is NaN\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "R44c-eKkHh7d",
        "outputId": "5ae51083-772b-474d-ead6-49fe09250350"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "x is NaN\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Important Notes:**\n",
        "\n",
        "NaN compares unequal to any number, including itself. So, NaN == NaN is False.\n",
        "Calculations involving NaN often result in NaN."
      ],
      "metadata": {
        "id": "LJIvbbd7Hq50"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = float('nan')\n",
        "y = float('nan')\n",
        "\n",
        "print( x == y) # prints False"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "X03qT75LHtjV",
        "outputId": "f5d73f82-99fb-40b8-b8ab-a1c7301ca880"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "False\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Infinity**\n",
        "\n",
        "In Python, math.inf represents positive infinity, and it is indeed displayed as inf when you print it.\n",
        "\n",
        "Here's a quick confirmation:"
      ],
      "metadata": {
        "id": "roPQubJCITFV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "\n",
        "positive_infinity = math.inf\n",
        "#positive_infinity = -math.inf # Negative infinity\n",
        "\n",
        "print(positive_infinity)  # Output: inf\n",
        "print(type(positive_infinity))  # Output: <class 'float'>"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IZOEnoJvIY8l",
        "outputId": "26d1889c-08a1-4c95-bc73-adea1f414bb4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-inf\n",
            "<class 'float'>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### **Key points to remember about math.inf:**\n",
        "\n",
        "- Floating-point: math.inf is a floating-point value, not an integer.\n",
        "- Comparisons: It's greater than any finite number.\n",
        "- Operations: Many mathematical operations involving infinity will result in infinity (or NaN in some cases, like infinity minus infinity).\n",
        "- Negative Infinity: For negative infinity, you would use -math.inf."
      ],
      "metadata": {
        "id": "fBPeS8CHIrIU"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "\n",
        "positive_infinity_1 = math.inf\n",
        "positive_infinity_2 = math.inf\n",
        "\n",
        "print(positive_infinity_1 - positive_infinity_2)  # Output: nan\n",
        "print(positive_infinity_1 * 2)  # Output: inf"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4UyVMaqIIzNl",
        "outputId": "b9bd4ff4-8be7-44ec-f933-2b437065a473"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "nan\n",
            "inf\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "\n",
        "positive_infinity = math.inf\n",
        "print(positive_infinity > 999999999999999999999999) #It's greater than any finite number."
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TfnMVuixJeYN",
        "outputId": "e397c29d-ea85-4816-ef87-90ce26cb3433"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "True\n"
          ]
        }
      ]
    }
  ]
}